package com.example.cp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    EditText name;
    EditText age;
    Spinner sex;
    EditText addr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name);
        age = findViewById(R.id.age);
        sex = findViewById(R.id.sex);
        addr = findViewById(R.id.addr);

        Button saveBtn = findViewById(R.id.add);
        Button queryBtn = findViewById(R.id.query);
        TextView msg = findViewById(R.id.msg);
        Button updateBtn = findViewById(R.id.update);
        Button deleteBtn = findViewById(R.id.delete);

        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentResolver cr = getContentResolver();
                Uri uri = Uri.parse("content://com.example.cp.UserInfoProvider/user");
                ContentValues values = new ContentValues();
                values.put("name",name.getText().toString());
                values.put("age",age.getText().toString());
                values.put("sex",sex.getSelectedItem().toString());
                values.put("addr",addr.getText().toString());
                cr.insert(uri,values);
            }
        });

        queryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ContentResolver cr = getContentResolver();
                Uri uri = Uri.parse("content://com.example.cp.UserInfoProvider/user");
                Cursor cursor = cr.query(uri, null, null, null);
                List<UserInfo> userInfos = new ArrayList<>();
                while (cursor.moveToNext()){
                    UserInfo userInfo = new UserInfo();
                    int id = cursor.getInt(0);
                    String name1 = cursor.getString(1);
                    Integer age = cursor.getInt(2);
                    String sex = cursor.getString(3);
                    String addr = cursor.getString(4);

                    userInfo.setId(id);
                    userInfo.setName(name1);
                    userInfo.setAge(age);
                    userInfo.setAddr(addr);
                    userInfo.setSex(sex);

                    userInfos.add(userInfo);
                }
                cursor.close();

                msg.setText(userInfos.toString());
            }
        });

        updateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentResolver cr = getContentResolver();
                Uri uri = Uri.parse("content://com.example.cp.UserInfoProvider/user");
                ContentValues values = new ContentValues();
                values.put("name",name.getText().toString());
                values.put("age",age.getText().toString());
                values.put("sex",sex.getSelectedItem().toString());
                values.put("addr",addr.getText().toString());
                cr.update(uri,values,null);
            }
        });

        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentResolver cr = getContentResolver();
                Uri uri = Uri.parse("content://com.example.cp.UserInfoProvider/user");
                cr.delete(uri,"name=?",new String[]{name.getText().toString()});
            }
        });
    }
}